
<template>
  <BasicLayout>
    <template #wrapper>
      <el-card class="box-card">
        <el-form ref="queryForm" :model="queryParams" :inline="true" class="demo-form-inline">
          <el-row>
            <el-col :span="5">
              <el-form-item label="订单状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择" clearable size="small">
                  <el-option
                    v-for="dict in statusOptions"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="适用地区" prop="province">
                <el-select
                  v-model="queryParams.province"
                  placeholder="请选择"
                  clearable
                  size="small"
                  remote
                  filterable
                  :remote-method="remoteMethod"
                  :loading="selectload"
                >
                  <el-option
                    v-for="(item, index) in cityList"
                    :key="index"
                    :label="item.name"
                    :value="item.name"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="订单编号" prop="order_number">
                <el-input
                  v-model="queryParams.order_number"
                  placeholder="请输入标题"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="9" class="itemStyle">
              <el-form-item label="提交时间">
                <el-date-picker
                  v-model="dateRange"
                  type="datetimerange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  size="small"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="5">
              <el-form-item label="手机号码" prop="phone">
                <el-input
                  v-model="queryParams.phone"
                  placeholder="请输入标题"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-form-item style="float:right">
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-row>
        </el-form>

        <el-button
          class="mb10"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
        <div style="display: flex">
          <div style="margin-right: 10px">订单总数：{{ total }}</div>
          <div>订单总金额：{{ $Prices(sum) }}（元）</div>
        </div>
        <el-table
          v-loading="loading"
          :data="sysOrderList"
          border
          @selection-change="handleSelectionChange"
          @sort-change="handleSortChang"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column
            type="index"
            header-align="center"
            width="50"
            label="序号"
            fixed="left"
            align="center"
          />
          <el-table-column
            header-align="center"
            label="订单编号"
            fixed="left"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.order_number }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column header-align="center" prop="subject" label="科目" />
          <el-table-column header-align="center" prop="province" label="适用地区" />-->
          <el-table-column label="订单金额" header-align="center" align="left" prop="price" width="100">
            <template slot-scope="scope">
              <span class="text-price">{{ $Prices(scope.row.price) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="订单返佣"
            header-align="center"
            align="left"
            prop="commission"
            width="100"
          >
            <template slot-scope="scope">
              <span class="text-price">{{ $Prices(scope.row.commission) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="customer" label="关联上级" header-align="center">
            <template slot-scope="scope">
              <span
                v-if="scope.row.customer && scope.row.customer.recommenders"
              >{{ scope.row.customer.recommenders.name }}</span>
              <span v-else>-</span>
            </template>
          </el-table-column>
          <el-table-column label="订单状态" header-align="center" align="left" prop="status">
            <template slot-scope="scope">
              <el-tag :type="tagType(scope.row.status)">{{ tagStatus(scope.row.status) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="买家信息" header-align="center" align="left" prop="customer">
            <template slot-scope="scope">
              <span>{{ scope.row.customer ? scope.row.customer.name : '' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="买家号码"
            header-align="center"
            align="left"
            prop="customer"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.customer ? scope.row.customer.phone : '' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="教师信息"
            header-align="center"
            align="left"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.customer && scope.row.customer.teach_recommenders ? scope.row.customer.teach_recommenders.username : '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="提交时间" align="center" prop="createdAt" width="155px">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createdAt) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <router-link :to="{name:'OrderView', params: {id:scope.row.id,type: 'online'}}">
                <el-button size="mini" type="text" icon="el-icon-view">查看</el-button>
              </router-link>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageIndex"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-card>
    </template>
  </BasicLayout>
</template>

<script>
import { onlineOrderList } from '@/api/admin/sys-order'
import { onlineStatusOptions, getTagType, getStatus } from '../utils'
import { getCity } from '@/api/admin/sys-course'
import { formatJson, prices } from '@/utils'
import { parseTime } from '@/utils/costum'
export default {
  name: 'SysOrderOnlineManage',
  components: {},
  data() {
    return {
      statusOptions: onlineStatusOptions,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      sum: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      sysOrderList: [],
      dateRange: [],
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        status: undefined,
        province: undefined,
        order_number: undefined,
        begin_time: undefined,
        end_time: undefined,
        phone: undefined
      },
      cityList: [],
      selectload: false
    }
  },
  created() {
    this.getList()
    getCity().then(res => {
      this.cityList = res?.data || []
    })
  },
  methods: {
    tagType(status) {
      return getTagType('online', status)
    },
    tagStatus(status) {
      return getStatus('online', status)
    },
    getList() {
      try {
        this.loading = true
        onlineOrderList(this.addDateRange(this.queryParams, this.dateRange))
          .then(response => {
            this.sysOrderList = response.data.list
            this.total = response.data.count ?? 0
            this.sum = response.data.sum ?? 0
          })
          .finally(() => {
            this.loading = false
          })
      } catch (error) {
        console.log(error)
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    parentIdFormat(row) {
      return this.selectItemsLabel(this.parentIdOptions, row.parentId)
    },
    // 文件
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 排序回调函数 */
    handleSortChang(column, prop, order) {
      prop = column.prop
      order = column.order
      if (this.order !== '' && this.order !== prop + 'Order') {
        this.queryParams[this.order] = undefined
      }
      if (order === 'descending') {
        this.queryParams[prop + 'Order'] = 'desc'
        this.order = prop + 'Order'
      } else if (order === 'ascending') {
        this.queryParams[prop + 'Order'] = 'asc'
        this.order = prop + 'Order'
      } else {
        this.queryParams[prop + 'Order'] = undefined
      }
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.consult_wz_id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form = row
      this.open = true
      this.title = '修改文章'
    },
    remoteMethod(query) {
      if (query !== '') {
        this.selectload = true
        getCity({ name: query }).then(res => {
          this.cityList = res?.data || []
          this.selectload = false
        })
      } else {
        this.cityList = []
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      // const queryParams = this.queryParams
      this.$confirm('是否确认导出所有参数数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = [
            '订单编号',
            '科目',
            '适用地区',
            '订单金额',
            '订单返佣',
            '关联上级',
            '订单状态',
            '买家信息',
            '教师信息',
            '提交时间'
          ]
          const filterVal = [
            'order_number',
            'subject',
            'province',
            'price',
            'commission',
            'customer',
            'status',
            'center',
            'teach',
            'createdAt'
          ]
          const list = this.sysOrderList.map(item => {
            return {
              order_number: item?.order_number,
              subject: item?.subject,
              province: item?.province,
              price: prices(item?.price),
              commission: prices(item?.commission),
              customer: item?.customer ? item?.customer?.name : '',
              status: this.tagStatus(item.status),
              center: item?.customer ? item?.customer?.name : '',
              teach: item?.customer
                ? item?.customer?.teach_recommenders?.username
                : '',
              createdAt: parseTime(item?.createdAt)
            }
          })
          const data = formatJson(filterVal, list)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '网课订单',
            autoWidth: true, // Optional
            bookType: 'xlsx' // Optional
          })
          this.downloadLoading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">
.box-card {
  .el-row {
    .el-col-5 {
      .el-form-item {
        margin-right: 0;
        .el-form-item__content {
          width: 70%;
        }
      }
    }
  }
}
</style>
